﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.NonlinearEquationSolvers
{
    /// <summary>
    /// This class provides the finding of roots of a polynomial by using the Euler-Tschebyschow 
    /// method.
    /// </summary>
    [Serializable]
    public class EulerTschebyschowRootFinder : AbstractDerivativeNeedRootFinder
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="EulerTschebyschowRootFinder"/> class.
        /// </summary>
        /// <param name="polynomial">The polynomial for finding the roots.</param>
        public EulerTschebyschowRootFinder(Polynomial polynomial)
            : base(polynomial)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="EulerTschebyschowRootFinder"/> class.
        /// </summary>
        /// <param name="polynomial">The polynomial for finding the roots.</param>
        public EulerTschebyschowRootFinder(SimplePolynomial polynomial)
            : base(polynomial)
        {
        }

        /// <summary>
        /// Find one root of the polynomial by using the Euler-Tschebyschow method. The x has to
        /// be choose useful to find a root.
        /// </summary>
        /// <param name="x">The startvalue of the approximation.</param>
        /// <returns>One root of the polynomial.</returns>
        public double FindRoots(double x)
        {
            return this.FindRoots(x, 1e-15, 1000);
        }

        /// <summary>
        /// Find one root of the polynomial by using the Euler-Tschebyschow method. The x has to
        /// be choose useful to find a root.
        /// </summary>
        /// <param name="x">The startvalue of the approximation.</param>
        /// <param name="iterations">The number of iterations to find a root.</param>
        /// <returns>One root of the polynomial.</returns>
        public double FindRoots(double x, int iterations)
        {
            return this.FindRoots(x, 1e-15, iterations);
        }

        /// <summary>
        /// Find one root of the polynomial by using the Euler-Tschebyschow method. The x has to
        /// be choose useful to find a root.
        /// </summary>
        /// <param name="x">The startvalue of the approximation.</param>
        /// <param name="precision">The precision of the result.</param>
        /// <param name="iterations">The number of iterations to find a root.</param>
        /// <returns>One root of the polynomial.</returns>
        public double FindRoots(double x, double precision, int iterations)
        {
            double tempuri = 0;
            Polynomial firstDerivation = this.Polynomial.Derivative();
            Polynomial secondDerivation = firstDerivation.Derivative();

            for (int i = 0; i < iterations; i++)
            {
                double s = -(this.Polynomial.SolveAt(x) / firstDerivation.SolveAt(x));
                double t = -0.5 * ((secondDerivation.SolveAt(x) * Math.Pow(s, 2)) / firstDerivation.SolveAt(x));

                tempuri = x;
                x += s + t;

                if (Math.Abs(tempuri - x) < precision)
                {
                    this.NeededIterations = i;
                    this.PrecisionError = false;
                    this.RelativeError = Math.Abs(tempuri - x);

                    return tempuri;
                }
            }

            this.PrecisionError = true;
            this.NeededIterations = iterations;
            this.RelativeError = Math.Abs(tempuri - x);

            return x;
        }
    }
}